#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
向现有admin添加业务模型
"""
import os
import sys
import django

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings_ultra_minimal')
django.setup()

from django.contrib import admin
from django.db import models

# 创建一些基本的业务模型
print("🏭 创建基本的MES业务模型...")

# 1. 客户模型
class Customer(models.Model):
    name = models.CharField("客户名称", max_length=100)
    code = models.CharField("客户编码", max_length=50, unique=True)
    contact = models.CharField("联系人", max_length=50, blank=True)
    phone = models.CharField("电话", max_length=20, blank=True)
    email = models.EmailField("邮箱", blank=True)
    address = models.TextField("地址", blank=True)
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    
    class Meta:
        verbose_name = "客户"
        verbose_name_plural = "客户管理"
        app_label = 'admin'
    
    def __str__(self):
        return f"{self.name} ({self.code})"

# 2. 产品模型
class Product(models.Model):
    name = models.CharField("产品名称", max_length=100)
    code = models.CharField("产品编码", max_length=50, unique=True)
    category = models.CharField("产品类别", max_length=50, blank=True)
    description = models.TextField("产品描述", blank=True)
    unit = models.CharField("单位", max_length=10, default="件")
    price = models.DecimalField("单价", max_digits=10, decimal_places=2, default=0)
    created_at = models.DateTimeField("创建时间", auto_now_add=True)
    
    class Meta:
        verbose_name = "产品"
        verbose_name_plural = "产品管理"
        app_label = 'admin'
    
    def __str__(self):
        return f"{self.name} ({self.code})"

# 3. 订单模型
class Order(models.Model):
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('processing', '处理中'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
    ]
    
    order_no = models.CharField("订单号", max_length=50, unique=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, verbose_name="客户")
    product = models.ForeignKey(Product, on_delete=models.CASCADE, verbose_name="产品")
    quantity = models.PositiveIntegerField("数量", default=1)
    status = models.CharField("状态", max_length=20, choices=STATUS_CHOICES, default='pending')
    order_date = models.DateTimeField("下单时间", auto_now_add=True)
    delivery_date = models.DateField("交货日期", null=True, blank=True)
    notes = models.TextField("备注", blank=True)
    
    class Meta:
        verbose_name = "订单"
        verbose_name_plural = "订单管理"
        app_label = 'admin'
        ordering = ['-order_date']
    
    def __str__(self):
        return f"{self.order_no} - {self.customer.name}"

# 4. 设备模型
class Equipment(models.Model):
    STATUS_CHOICES = [
        ('running', '运行中'),
        ('maintenance', '维护中'),
        ('idle', '空闲'),
        ('fault', '故障'),
    ]
    
    name = models.CharField("设备名称", max_length=100)
    code = models.CharField("设备编码", max_length=50, unique=True)
    model = models.CharField("设备型号", max_length=50, blank=True)
    location = models.CharField("设备位置", max_length=100, blank=True)
    status = models.CharField("设备状态", max_length=20, choices=STATUS_CHOICES, default='idle')
    purchase_date = models.DateField("采购日期", null=True, blank=True)
    last_maintenance = models.DateField("上次维护", null=True, blank=True)
    notes = models.TextField("备注", blank=True)
    
    class Meta:
        verbose_name = "设备"
        verbose_name_plural = "设备管理"
        app_label = 'admin'
    
    def __str__(self):
        return f"{self.name} ({self.code})"

# 5. 员工模型
class Employee(models.Model):
    DEPARTMENT_CHOICES = [
        ('production', '生产部'),
        ('quality', '质量部'),
        ('warehouse', '仓库'),
        ('management', '管理部'),
        ('maintenance', '维护部'),
    ]
    
    name = models.CharField("姓名", max_length=50)
    employee_id = models.CharField("工号", max_length=20, unique=True)
    department = models.CharField("部门", max_length=20, choices=DEPARTMENT_CHOICES)
    position = models.CharField("职位", max_length=50, blank=True)
    phone = models.CharField("电话", max_length=20, blank=True)
    email = models.EmailField("邮箱", blank=True)
    hire_date = models.DateField("入职日期", null=True, blank=True)
    is_active = models.BooleanField("是否在职", default=True)
    
    class Meta:
        verbose_name = "员工"
        verbose_name_plural = "员工管理"
        app_label = 'admin'
    
    def __str__(self):
        return f"{self.name} ({self.employee_id})"

# 注册模型到admin
@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'contact', 'phone', 'created_at']
    list_filter = ['created_at']
    search_fields = ['name', 'code', 'contact']
    readonly_fields = ['created_at']

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'category', 'unit', 'price', 'created_at']
    list_filter = ['category', 'created_at']
    search_fields = ['name', 'code', 'category']
    readonly_fields = ['created_at']

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ['order_no', 'customer', 'product', 'quantity', 'status', 'order_date']
    list_filter = ['status', 'order_date']
    search_fields = ['order_no', 'customer__name', 'product__name']
    readonly_fields = ['order_date']
    date_hierarchy = 'order_date'

@admin.register(Equipment)
class EquipmentAdmin(admin.ModelAdmin):
    list_display = ['name', 'code', 'model', 'location', 'status', 'last_maintenance']
    list_filter = ['status', 'location']
    search_fields = ['name', 'code', 'model']

@admin.register(Employee)
class EmployeeAdmin(admin.ModelAdmin):
    list_display = ['name', 'employee_id', 'department', 'position', 'is_active']
    list_filter = ['department', 'is_active', 'hire_date']
    search_fields = ['name', 'employee_id', 'position']

print("✅ 业务模型创建完成！")
print("📋 已添加以下业务模块:")
print("  - 客户管理")
print("  - 产品管理") 
print("  - 订单管理")
print("  - 设备管理")
print("  - 员工管理")
print("\n💡 现在您可以在管理后台看到这些业务功能了！") 